package com.hmsm.sys.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.web.servlet.ModelAndView;

import com.hmsm.sys.bean.LocalDept;
import com.hmsm.sys.bean.LocalUser;
import com.hmsm.sys.controller.OrgController;
import com.hmsm.sys.service.IOrgService;
import com.hmsm.sys.service.IUserService;
import com.lzyyj.gmis.base.webui.ICurrentUserAware;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings({ "serial", "rawtypes" })
public class OrgAction extends ActionSupport implements SessionAware, ICurrentUserAware<LocalUser>{
	
	@Resource
	private OrgController orgController;

	private Map<String, Object> mapResult = new HashMap<String, Object>();
	
	public Map<String, Object> getMapResult() {
		return mapResult;
	}

	public void setMapResult(Map<String, Object> mapResult) {
		this.mapResult = mapResult;
	}
	
	private List<Object> listResult = new ArrayList<Object>();
	
	public List<Object> getListResult() {
		return listResult;
	}

	public void setListResult(List<Object> listResult) {
		this.listResult = listResult;
	}
	
	@Resource
	private OrgTreeHelper orgTreeHelper;
	
	private Map<String, Object> session;
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	private LocalUser currentUser;

	@Override
	public void setCurrentUser(LocalUser currentUser) {
		this.currentUser = currentUser;
	}

	private String nodeId;
	
	public String getNodeId() {
		return nodeId;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}
	
	private LocalDept parent;

	public LocalDept getParent() {
		return parent;
	}

	public void setParent(LocalDept parent) {
		this.parent = parent;
	}

	@Resource
	private IOrgService orgService;

	@Resource
	private IUserService userService;
	
	private List<LocalDept> parentSelectList;

	public List<LocalDept> getParentSelectList() {
		return parentSelectList;
	}

	public void setParentSelectList(List<LocalDept> parentSelectList) {
		this.parentSelectList = parentSelectList;
	}
	
	private String updDeptIds;

	public void setUpdDeptIds(String updDeptIds) {
		this.updDeptIds = updDeptIds;
	}

	public List<Integer> getUpdDeptIds() {
		List<Integer> list = new ArrayList<Integer>();
		if (updDeptIds!=null){
			for(String id:updDeptIds.split(",")){
				if(id.length()>0){
					list.add(Integer.parseInt(id));
				}
			}
		}
		return list;
	}
	
	private String addDeptIds;

	public void setAddDeptIds(String addDeptIds) {
		this.addDeptIds = addDeptIds;
	}

	public List<Integer> getAddDeptIds() {
		List<Integer> list = new ArrayList<Integer>();
		if (addDeptIds!=null){
			for(String id:addDeptIds.split(",")){
				if(id.length()>0){
					list.add(Integer.parseInt(id));
				}
			}
		}
		return list;
	}
	
	private String updUserIds;

	public void setUpdUserIds(String updUserIds) {
		this.updUserIds = updUserIds;
	}

	public List<Integer> getUpdUserIds() {
		List<Integer> list = new ArrayList<Integer>();
		if (updUserIds!=null){
			for(String id:updUserIds.split(",")){
				if(id.length()>0){
					list.add(Integer.parseInt(id));
				}
			}
		}
		return list;
	}
	
	private String addUserIds;

	public void setAddUserIds(String addUserIds) {
		this.addUserIds = addUserIds;
	}

	public List<Integer> getAddUserIds() {
		List<Integer> list = new ArrayList<Integer>();
		if (addUserIds!=null){
			for(String id:addUserIds.split(",")){
				if(id.length()>0){
					list.add(Integer.parseInt(id));
				}
			}
		}
		return list;
	}
	
	private String delUserIds;

	public void setDelUserIds(String delUserIds) {
		this.delUserIds = delUserIds;
	}

	public List<Integer> getDelUserIds() {
		List<Integer> list = new ArrayList<Integer>();
		if (delUserIds!=null){
			for(String id:delUserIds.split(",")){
				if(id.length()>0){
					list.add(Integer.parseInt(id));
				}
			}
		}
		return list;
	}
	
	private String delDeptIds;

	public void setDelDeptIds(String delDeptIds) {
		this.delDeptIds = delDeptIds;
	}

	public List<Integer> getDelDeptIds() {
		List<Integer> list = new ArrayList<Integer>();
		if (delDeptIds!=null){
			for(String id:delDeptIds.split(",")){
				if(id.length()>0){
					list.add(Integer.parseInt(id));
				}
			}
		}
		return list;
	}

	public OrgAction(){
		super();
	}
	
	public String manage() {
		ModelAndView modelAndView = orgController.manage(currentUser, session);
		
		return modelAndView.getViewName();
	}
	
	public String sync(){
		ModelAndView modelAndView = orgController.sync(getAddDeptIds(), getUpdDeptIds(), getUpdUserIds(), getAddUserIds(), getDelUserIds(), getDelDeptIds(), currentUser, session);
		
		boolean success = (Boolean) modelAndView.getModel().get("success");
		if(success) {
			Integer addDeptCount = (Integer) modelAndView.getModel().get("addDeptCount");
			getMapResult().put("addDeptCount", addDeptCount);
			
			String message = (String) modelAndView.getModel().get("message");
			mapResult.put("message", message);
		} else {
			String errorMessage = (String) modelAndView.getModel().get("errorMessage");
			mapResult.put("errorMessage", errorMessage);
		}
		mapResult.put("success", success);
		
		return "json";
	}

}
